
from django.shortcuts import render,redirect
from app01 import models



def depart_list(request):
    queryset = models.Department.objects.all()# 查询所有部门(列表,里面是对象)

    return render(request, 'depart_list.html', {'queryset': queryset})
def depart_add(request):
    """添加部门"""
    if request.method == 'POST':
        # 获取用户提交的数据
        title = request.POST.get('title')
        # 添加到数据库
        models.Department.objects.create(title=title)
        # 重定向到部门列表页面
        return redirect('/depart/list/')
    return render(request, 'depart_add.html')

def depart_delete(request):
    """删除部门"""
    nid = request.GET.get('nid')
    models.Department.objects.filter(id=nid).delete()
    return redirect('/depart/list/')
def depart_edit(request,nid):
    if request.method == 'GET':
        # 根据nid获取到要编辑的部门对象
        row_object = models.Department.objects.get(id=nid)
        return render(request, 'depart_edit.html', {'row_object': row_object})
    elif request.method == 'POST':
        # 获取用户修改后的数据
        title = request.POST.get('title')
        # 根据nid获取到要编辑的部门对象
        row_object = models.Department.objects.get(id=nid)
        row_object.title = title
        row_object.save()
        return redirect('/depart/list/')
